using MediatR;
using MeetingManagement.Application.Commands.Meetings.BookMettingRoom;
using MeetingManagement.Application.Commands.Meetings.CancelMeeting;
using MeetingManagement.Application.Queries.Meetings;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace MeetingManagement.WebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class MeetingsController : ControllerBase
    {
        private readonly IMediator _mediator;
        public MeetingsController(IMediator mediator) { _mediator = mediator; }

        [HttpPost("book")]
        public async Task<IActionResult> Book([FromBody] BookMeetingRoomCommand cmd)
            => Ok(await _mediator.Send(cmd));

        [HttpGet("my")]
        public async Task<IActionResult> MyMeetings()
            => Ok(await _mediator.Send(new GetMyMeetingsQuery { UserId = User.GetUserId() }));

        [HttpGet("room/{roomId}")]
        public async Task<IActionResult> RoomMeetings(Guid roomId)
            => Ok(await _mediator.Send(new GetMeetingRoomBookingsQuery { MeetingRoomId = roomId }));

        [HttpPost("cancel/{meetingId}")]
        public async Task<IActionResult> Cancel(Guid meetingId)
            => Ok(await _mediator.Send(new CancelMeetingCommand { MeetingId = meetingId, UserId = User.GetUserId() }));

        [HttpGet("invite/{meetingId}")]
        public IActionResult GetInviteUrl(Guid meetingId)
        {
            var url = Url.Action("Join", "Meetings", new { meetingId }, Request.Scheme);
            return Ok(new { inviteUrl = url });
        }
    }
}
